#include <bits/stdc++.h>

#define N 10005

using namespace std;

vector <int> G[N];
queue <int> q;
int in[N], tim[N], f[N];

int main(){
    int n;
    cin >> n;
    memset(in, 0, sizeof(in));
    for(int i = 1; i <= n; i++){
        int no; int tmp;
        scanf("%d", &no);
        scanf("%d",  &tim[no]);
        while(1){
            scanf("%d", &tmp);
            if(tmp == 0) break;
            G[tmp].push_back(no);
            in[no]++;
        }
    }
    for(int i = 1; i <= n; i++){
        if(in[i] == 0){
            q.push(i);
            f[i] = tim[i];
        }
    }
    while(!q.empty()){
        int tmp = q.front();    q.pop();
        for(int i : G[tmp]){
            in[i]--;
            if(in[i] == 0){
                q.push(i);
            }
            f[i] = max(f[i], f[tmp] + tim[i]);
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        ans = max(ans, f[i]);
    }
    cout << ans;
    return 0;
}